package com.bai.statistics.service.impl;

import com.bai.statistics.client.UcenterClient;
import com.bai.statistics.entity.Daily;
import com.bai.statistics.mapper.DailyMapper;
import com.bai.statistics.service.DailyService;
import com.bai.utils.R;
import com.bai.utils.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author bai
 * @since 2023-08-21
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 统计某一天注册人数,生成统计数据
     *
     * @param day
     */
    @Override
    public void registerCount(String day) {
        //TODO:添加记录之前先删除表中相同日期的数据
        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("data_calculated", day);
        baseMapper.delete(queryWrapper);

        R registerR = ucenterClient.cuntRegister(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");
        int loginNum = RandomUtils.nextInt(100, 200);
        int videoViewNum = RandomUtils.nextInt(100, 200);
        int courseNum = RandomUtils.nextInt(100, 200);

        Daily daily = new Daily();
        daily.setRegisterNum(countRegister);
        daily.setDateCalculated(day);
        daily.setLoginNum(loginNum);
        daily.setCourseNum(courseNum);
        daily.setVideoViewNum(videoViewNum);
        baseMapper.insert(daily);
    }

    /**
     * 图表显示，返回两部分数据，日期json数据，数量json数据
     *
     * @param type
     * @param begin
     * @param end
     */

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //根据条件查询数据
        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("data_calculated", begin, end);
        queryWrapper.select("data_calculated", type);
        List<Daily> dailies = baseMapper.selectList(queryWrapper);

        List<String> dateList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();
        for (Daily daily : dailies) {
            dateList.add(daily.getDateCalculated());
            switch (type) {
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList",dateList);
        map.put("numDataList",numDataList);
        return map;
    }
}
